package com.gitee.l0km.codegen.decorator;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.gitee.l0km.codegen.base.generator.GeneratorConfiguration;
import com.gitee.l0km.com4j.base.ClassLoaderUtils;
import com.gitee.l0km.javadocreader.JavadocReader;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * decorator生成器参数对象
 * @author guyadong
 *
 */
public class MXBeanConfiguration extends GeneratorConfiguration implements MXBeanConstants{
	public static final String DECORATOR_FOLDER = "mxbean";
	private List<Class<?>> refClasses;
	private String sourcepath;
	private String classpath;
	private final Set<String> members = Sets.newLinkedHashSet();
	private final Map<Class<?>, List<String>> excludeFields = Maps.newHashMap();
	private final Map<Class<?>, List<String>> includeFields = Maps.newHashMap();
	private final Map<Class<?>, List<String>> excludeMethods = Maps.newHashMap();
	private final Map<Class<?>, List<String>> includeMethods = Maps.newHashMap();
	public static final MXBeanConfiguration MXBCONFIG = new MXBeanConfiguration();

	private MXBeanConfiguration() {
		super();
		// 指定模板文件夹
		this.defaultValue.setProperty(TEMPLATE_FOLDER_OPTION_LONG, DECORATOR_FOLDER);
		// 指定sourcepath的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(SOURCE_PREFIX_OPTION_LONG,"");
		// 指定classpath的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(CLASS_PATH_OPTION_LONG,"");
		// 指定members的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(MEMBERS_OPTION_LONG,MEMBER_FIELD);
		// 指定excludeFields的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(EXCLUDE_FIELDS_OPTION_LONG,new String[0]);
		// 指定includeFields的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(INCLUDE_FIELDS_OPTION_LONG,new String[0]);
		// 指定excludeMethods的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(EXCLUDE_METHODS_OPTION_LONG,new String[0]);
		// 指定includeMethods的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(INCLUDE_METHODS_OPTION_LONG,new String[0]);
	}
	
	@Override
	public void loadConfig(Options options, CommandLine cmd) throws ParseException {
		super.loadConfig(options, cmd);
		sourcepath = getProperty(SOURCE_PREFIX_OPTION_LONG);
		classpath = ClassLoaderUtils.normalizeClasspath((String)getProperty(CLASS_PATH_OPTION_LONG));
		JavadocReader.setSourcepath(sourcepath);
		JavadocReader.setClasspath(classpath);
		JavadocReader.resetRoot();
		refClasses = getClasses(REFERENCE_CLASS_OPTION_LONG, classpath, false);
		members.addAll(getListProperty(MEMBERS_OPTION_LONG));
		readClassEntry(EXCLUDE_FIELDS_OPTION_LONG, excludeFields);
		readClassEntry(INCLUDE_FIELDS_OPTION_LONG, includeFields);
		readClassEntry(EXCLUDE_METHODS_OPTION_LONG, excludeMethods);
		readClassEntry(INCLUDE_METHODS_OPTION_LONG, includeMethods);
	}

	/**
	 * @return refClasses
	 */
	public List<Class<?>> getRefClasses() {
		return refClasses;
	}

	/**
	 * @return sourcepath
	 */
	public String getSourcepath() {
		return sourcepath;
	}

	/**
	 * @return classpath
	 */
	public String getClasspath() {
		return classpath;
	}
	
	public boolean isNeedField(){
		return members.contains(MEMBER_FIELD);
	}
	public boolean isNeedMethod(){
		return members.contains(MEMBER_METHOD);
	}

	public static String getDecoratorFolder() {
		return DECORATOR_FOLDER;
	}

	public Set<String> getMembers() {
		return members;
	}

	public Map<Class<?>, List<String>> getExcludeFields() {
		return excludeFields;
	}

	public Map<Class<?>, List<String>> getIncludeFields() {
		return includeFields;
	}

	@Override
	public Map<Class<?>, List<String>> getExcludeMethods() {
		return excludeMethods;
	}

	@Override
	public Map<Class<?>, List<String>> getIncludeMethods() {
		return includeMethods;
	}
}
